<?php
class Controller_LoginController {
	const SESSION_USER_KEY = "USER_KEY";
	const SESSION_USER_LOGIN_ERROR = "LOGIN_ERROR";

	public static function login($username, $password) {
		$userDao = new Dao_UserDAO();
		$user = $userDao->login($username, $password);
		if ($user !== null) {
			$_SESSION[self::SESSION_USER_KEY] = $user;
			return true;
		} else {
			return false;
		}
	}

	public static function getLoginUser() {
		if (self::isLogin()) {
			return $_SESSION[self::SESSION_USER_KEY];
		} else {
			return null;
		}
	}

	public static function isLogin() {
		return isset($_SESSION[self::SESSION_USER_KEY])
		&& $_SESSION[self::SESSION_USER_KEY] !== null;
	}

	public static function logout() {
		if ( isset($_SESSION[self::SESSION_USER_KEY])) {
			unset($_SESSION[self::SESSION_USER_KEY]);
		}
	}

	public static function createPassword($username, $password) {
		$username = sha1($username);
		$password = sha1($password);
		$passnew = sha1($username . $password);
		return $passnew;
	}

	public static function getLoginError() {
		if (isset($_SESSION[self::SESSION_USER_LOGIN_ERROR])) {
			return $_SESSION[self::SESSION_USER_LOGIN_ERROR];
		} else {
			return "";
		}
	}

	public static function unsetLoginError() {
		if (isset($_SESSION[self::SESSION_USER_LOGIN_ERROR])) {
			unset($_SESSION[self::SESSION_USER_LOGIN_ERROR]);
		}
	}

	public static function setLoginError($error) {
		$_SESSION[self::SESSION_USER_LOGIN_ERROR] = $error;
	}
}